<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>考勤查询</title>
<jsp:include page="${home}/assets/common_inc_new.jsp"  flush="true"></jsp:include>
<script type="text/javascript" src="${home}/system/common/scripts/zDialog.js"></script>
<script type="text/javascript" src="${home}/system/common/scripts/utils.js"></script>
<link rel="stylesheet" type="text/css" href="../css/index.css">
<link rel="stylesheet" type="text/css" href="../css/add.css">
<link rel="stylesheet" type="text/css" href="../css/details.css">

<script type="text/javascript">
function getYMD(){
  var date = new Date();
  var seperator1 = "-";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
       month = "0" + month;
   }
  if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
   }
  return date.getFullYear() + seperator1 + month + seperator1 + strDate;
}

var result;
var page_current=1;
var page_size=0;
var page_interval=10;

function ui_listReqInfos() {
	page_size=$("#txtPageNum").val();
	queryUsers();

	// var queryCallBack=function(reqInfos,callResult){
	// 	if(callResult.response_code == '0'){
	// 		$.dialog.tips("查询成功...");
	// 		ui_addReqInfo(reqInfos.content.result.list);
	// 		var htmlMsg2=OutPageListAjax(page_size,page_current, reqInfos.content.result.total,  pageChangeCallback, page_interval);
	// 		$("#PageContent").html(htmlMsg2);
	// 		page_current=1;
	// 	}
	// }
	// listReqInfos(queryCallBack);
}

function pageChangeCallback(pageid){
	page_current = pageid;
	ui_listReqInfos();
}

function listReqInfos(callBackList){
	var head={
		"service_name":"cn.dy.workatten.service.WorkAttenService",
		"operation_name":"queryAuditWorkAttendList"
	}
	var attenDay = $("#attenMonth").val() + "-" + $("#attenDay").val();
	if(right_click_node.id==null || right_click_node.id<=0||right_click_node.id==""){
		alert("请选择部门");
		throw SyntaxError();
	}
	var param={
		"dept_id": right_click_node.id,
		"beginTime": attenDay,
		"endTime": attenDay,
		"pageNum": page_current,
		"pageSize": page_size
	}
	var myCallBack=function(data,callResult){
	if(callBackList){
		callBackList(data,callResult);
		}
	}
	var options={
		 "handleError": false,
		"showProgressBar":false,
		"timeout":60000*10
	 };
	$.ServiceAgent.JSONInvoke(head,param,myCallBack,options);
}



function cancelModel(id) {
  $("#" + id).fadeOut();
}

function obj2Str(obj) {
  return JSON.stringify(obj).replace(/"/g, "'");
}

function buildClickTd(item, row, column) {
  var itemStr = obj2Str(item);
  return "<td align=\"center\" class=\"click-td\" onclick=\"choose(" + itemStr + ", " + row + ", '" + column + "');\">"
  + "<img src=\"../assets/v.png\" class=\"v\" id=\"v_" + row + "_" + column + "\">"
  + "</td>";
}

function buildInputTd(value) {
  return "<td align=\"center\">"
  + "<input class=\"td-input\" type=\"text\" value=\"" + value + "\">"
  + "</td>";
}

function buildTd(value) {
  return "<td align=\"center\">" + value + "</td>";
}

function buildTableDataUI(data){
  $("#resultgrid tr:gt(1)").remove();
  for (var i = 0; i < data.length; i++) {
    var item = data[i];
    var html = "<tr>"
    + buildTd(i + 1)
    + buildTd(item.name)
	+ buildClickTd(item, item.id, 'am')
	+ buildClickTd(item, item.id, 'pm')
    // + buildClickTd(item, i, 'am')
    // + buildClickTd(item, i, 'pm')
    + buildClickTd(item, i, 'night')
    + buildClickTd(item, i, 'day')
    + buildInputTd(item.extValue ? item.extValue : 0 )
    + buildInputTd(item.nightValue ? item.nightValue : 0)
    + "</tr>";
    $("#resultgrid").append(html);
  }
}

function choose(user, row, column) {
  if (isNaN(column)) {
    return;
  }
  var v = "#v_" + row + "_" + column;
  //$(v).toggle();
}

var diag;
var approvalBtnId
function queryApproval(id) {
    approvalBtnId = id;
    diag = new Dialog();
    diag.Width = 800;
    diag.Height = 500;
    diag.Title = "";
    diag.URL = "approval.jsp";
    diag.ShowMessageRow = false;
    diag.show();
}

function addApproval(approval) {
  console.log(approval);
  console.log(approvalBtnId);
  var approvalHtml = "<div class=\"approval-person\">" + approval.name + "</div>";
  $("#" + approvalBtnId).before(approvalHtml);
}

function initChoose(data) {
  var arr1 = [0, 1, 4, 6];
  for (var index of arr1) {
    data[index].am = 0;
    data[index].pm = 1;
    data[index].day = 3;
  }
  var arr2 = [3, 5, 7];
  for (var index of arr2) {
    data[index].am = 0;
    data[index].night = 2;
    data[index].day = 3;
  }
  for (var i = 0; i < data.length; i++) {
    var item = data[i];
    choose(item, i, item.am);
    choose(item, i, item.pm);
    choose(item, i, item.night);
    choose(item, i, item.day);
  }
}


function doExport() {
	if(right_click_node.id==null || right_click_node.id<=0||right_click_node.id==""){
		alert("请选择部门");
		throw SyntaxError();
	}
	var dept_id = right_click_node.id;
	var monthStr = $("#attenMonth").val() + "-" + $("#attenDay").val();
	var searchStr = $("#searchStr").val();
	if (monthStr && monthStr.length > 7){
		monthStr = monthStr.substring(0, 8);
	} else{
		monthStr = getYMD().substring(0, 8);
	}
	window.location.href = "export.jsp?dept_id=" + dept_id + "&monthStr=" + monthStr + "&searchStr=" + encodeURI(searchStr);
} 

function beforeDay(){
	var attenDay = parseInt($("#attenDay").val());
	var countDay = (attenDay - 1);
	if (countDay >= 0 && countDay <= 9) {
      countDay = "0" + countDay;
   }
	if(attenDay > 1){
		$("#attenDay").val(countDay);
	}
}

function afterDay(){
	var attenDay = parseInt($("#attenDay").val());
	var countDay = (attenDay + 1);
	if (countDay >= 0 && countDay <= 9) {
      countDay = "0" + countDay;
   }
	if(attenDay < 31){
		$("#attenDay").val(countDay);
	}
}

var attendData = [];
function queryUsers() {
  var head={
    "service_name":"cn.dy.workatten.service.WorkAttenService",
    "operation_name":"getContactInfoByDept"
  };
  if(right_click_node.id==null || right_click_node.id<=0||right_click_node.id==""){
		alert("请选择部门");
		throw SyntaxError();
	}
  var param={
    "dept_id": right_click_node.id,
    "query": $("#searchStr").val(),
    "pageNum": page_current,
	"pageSize": page_size
  };
  var options = {
    "handleError": false,
    "showProgressBar":false,
    "timeout":60000*10
  };
  var cb = function(data, result) {
    if(data != null){
      $.dialog.tips("查询成功...");
      console.log('--------data-----------');
      console.log(data);
      attendData = data.content.result;
      buildTableDataUI(data.content.result);
      getWorkAttendDetail();

	  // 
	  var htmlMsg2=OutPageListAjax(page_size,page_current, data.content.total,  pageChangeCallback, page_interval);
	  $("#PageContent").html(htmlMsg2);
	  page_current=1;
    }
  }
  $.ServiceAgent.JSONInvoke(head, param, cb, options);
}

var workAttendDetail = {};
function getWorkAttendDetail() {
  var head={
    "service_name":"cn.dy.workatten.service.WorkAttenService",
    "operation_name":"getWorkAttendDetailByDay"
  };
  var monthStr = $("#attenMonth").val() + "-" + $("#attenDay").val();
   if(right_click_node.id==null || right_click_node.id<=0||right_click_node.id==""){
		alert("请选择部门");
		throw SyntaxError();
	}
  var param={
	  "dept_id": right_click_node.id,
	  "monthStr": monthStr,
	  "searchStr": $("#searchStr").val(),
      "pageNum": page_current,
	  "pageSize": page_size
	  //"pageNum": 0,
	  //"pageSize": 0
  };
  var options = {
    "handleError": false,
    "showProgressBar":false,
    "timeout":60000*10
  };
  var cb = function(data, result) {
    if(data.response_code == 0){
      $.dialog.tips("查询成功...");
      console.log('==================');
      console.log(data);
      workAttendDetail = data.content;
	  var attendApplication = data.content.attendApplication;
	  // $("#deptName").text(attendApplication.deptName);
	  // $("#current_date").text(attendApplication.attendanceDayStr);
      initData(data.content);
      // $("#applicant").text(data.content.attendApplication.staffName);
    } else if(data.response_code == -2){
		// 考勤不存在

	} else{
		alert(data.response_desc);
	}
  }
  $.ServiceAgent.JSONInvoke(head, param, cb, options);
}


function buildRecordsMap(data) {
  var map = {};
  for (var i = 0; i < data.attendRecordList.length; i++) {
    var record = data.attendRecordList[i];
    if (!map[record.staff_id]) {
      map[record.staff_id] = [];
    }
    map[record.staff_id].push(record);
  }
  return map;
}

var typeScheduleMap ={
  "1": 'am',
  "2": 'pm',
  "4": 'night',
  "8": 'day'
};

function initData(data) {
  var recordsMap = buildRecordsMap(data);
  for (var i = 0; i < attendData.length; i++) {
    var attend = attendData[i];
    var records = recordsMap[attend.id];
    if (records) {
      for (var j = 0; j < records.length; j++) {
        var record = records[j];
        if (record.scheduleCheckType) {
          var scheduleArr = record.scheduleCheckType.split(',');
          if (scheduleArr && scheduleArr.length) {
            for (var k = 0; k < scheduleArr.length; k++) {
              var type = scheduleArr[k];
              var schedule = typeScheduleMap[type];
			  var v = "#v_" + record.staff_id + "_" + schedule;
              // var v = "#v_" + i + "_" + schedule;
			  console.log('---'+v);
              record[schedule] = type;
              $(v).show();
            }
          }
        }
      }
    }
  }
}


$(function() {
  var ymd = getYMD();
  $("#attenMonth").val(ymd.substring(0,7));
  $("#attenDay").val(ymd.substring(8,10));

  // queryUsers();
});
</script>

</head>
<body class="mainbody">
	<!-- 导航栏 -->
	<div class="location">
		<a href="javascript:;" class="home"><i></i><span>考勤查询</span>
		</a>
	</div>

	<div class="toolbar-wrap">
    <div id="floatHead" class="toolbar" style="padding-bottom: 20px;">
      <div class="r-list" style="float: none;">
		<input type="hidden" name="部门id" id="dept_id" class="operation_date" placeholder="部门id" />
		<input type="text" name="txtdept" id="txtdept" for="txtdept" placeholder="请选择部门" style="float: left" sucmsg=" " style="width: 150px;" class="keyword date" readonly="readonly" onclick="ShowAction()" />
        <span class="operation_time">考勤月份:</span>
        <input name="attenMonth" type="text" id="attenMonth"
          class="keyword date"
          onfocus="WdatePicker({dateFmt:&#39;yyyy-MM&#39;})"
          datatype="*1-50" errormsg="请选择正确的日期" sucmsg=" " nullmsg=" "placeholder="日期"/>
        <input type="text" name="searchStr" id="searchStr" for="searchStr" style="width: 200px" class="keyword date" placeholder="姓名、身份证号、警号"  />
		<a id="lbtnSearch" class="btn-search" onclick="ui_listReqInfos();"><i></i><span>查询</span></a>
        <!-- <a id="lbtnSearch" class="reset" onclick="doExport();"><span>导出</span></a> -->
      </div>

	  <div class="l-list">
		<ul class="icon-list">
			<li>
				<a id="lbtnAdd" class="save" onclick="doExport();"><i></i><span>导出</span></a>
			</li>
		</ul>
	  </div>

    </div>
    <div class="toolbar-ctrl">
      <button type="button" class="ctrl-btn" onclick="beforeDay()">前一天</button>
      <button type="button" class="ctrl-btn" onclick="afterDay()">后一天</button>
      <span class="ctrl-date">日期: </span>
      <input class="ctrl-input" name="begin_time" type="text" id="attenDay"
        onfocus="WdatePicker({dateFmt:&#39;dd&#39;})"
        datatype="*1-50" errormsg="请选择正确的日期" sucmsg=" " nullmsg=" " placeholder="日期"/>
      <button type="button" class="ctrl-btn" onclick="ui_listReqInfos()">跳转</button>
    </div>
    <table width="100%" border="0" cellpadding="0" cellpadding="0" class="ltable" id="resultgrid">
			<tr>
				<th width="10%" rowspan="2">序号</th>
				<th width="10%" rowspan="2" class="name-date">
          <span class="name">姓名</span>
          <span class="datetxt">日期</span>
        </th>
        <th width="13%" colspan="6">1日</th>
			</tr>
      <tr>
				<th width="13%">上午</th>
				<th width="13%">下午</th>
				<th width="13%">夜班</th>
				<th width="13%">早中晚班</th>
				<th width="13%">额外分值(分)</th>
				<th width="13%">0点后(时)</th>
			</tr>
		</table>
	</div>

	<!--内容底部-->
	<div class="line30"></div>
	<div class="pagelist">
		<div class="l-btns">
			<span>显示</span>
			<input type="text" name="txtPageNum" value="10"
			onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;txtPageNum\&#39;,\&#39;\&#39;)&#39;, 0)"
			onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;"
					id="txtPageNum" class="pagenum"
					onkeydown="return checkNumber(event);"/>
			<span>条/页</span>
		</div>
		<div id="PageContent" class="default">
		</div>
	</div>

	<!-- 部门选择弹出框 -->
	<script id="recharge-tpl" type="text/template">
		<div id="role" style="width: 250px; float: left; ; height: 300px;background: #eeeeee">
			<div class="title">部门列表</div>
			<div id="treeParentArea" valign=top style="width: 100%;height: 500px;">
				<ul id="tree" class="ztree" style="overflow:auto;margin:0px;height:698px"></ul>
			</div>
		</div>
	</script>

	<script type="text/javascript">
		 $(function(){
			getCurrentDept();
		});

		function ShowAction() {
			var param = {'0':'模版描述'};
			var tit = "部门选择";
			jsdialog(tit, RpTpl($("#recharge-tpl").html(), param), "", "None", function (data) {  alert(data);},
				function (data) {
					$("#txtdept").val(right_click_node.name);
					$("#dept_id").val(right_click_node.id);
					
					ui_listReqInfos();
					//queryUsers();
				},
				function () {
					createTree();
				});
		}
		function RpTpl(tpl, obj) {
			var re = tpl;
			if (!tpl || tpl.length <= 0) return re;
			if (!obj || typeof obj != "object") return re;
			for (k in obj) {
				re = re.replace("{#" + k + "#}", obj[k]);
			}
			return re;
		}
	</script>

	<script type="text/javascript"
			src="/xyzg/system/common/scripts/utils.js"></script>
	<script type="text/javascript"
			src="/xyzg/system/common/scripts/zDialog.js"></script>
	<%--<script type="text/javascript" src="/xyzg/scripts/lhgdialog/lhgdialog.js?skin=idialog"></script>--%>
	<script type="text/javascript" src="/xyzg/scripts/zTree/js/jquery.ztree.all-3.5.min.js"></script>
	<script src="/xyzg/workatten/attendance/deptTreeJs.js"></script>
	<link rel="stylesheet" href="/xyzg/scripts/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">


</body>
</html>
